package sy.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import sy.jackson.SysBuildingMixin;
import sy.jackson.SysProjectMixin;
import sy.jackson.SysUnitMixin;
import sy.jackson.SysUserMixin;
import sy.model.IotTalkBackURI;
import sy.model.SysAccount;
import sy.model.SysBuilding;
import sy.model.SysDivision;
import sy.model.SysProject;
import sy.model.SysUnit;
import sy.service.WYBusinessServiceI;
import sy.util.HttpUtils;
import sy.util.ResultJson;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@Controller
@RequestMapping("v1/wyBusiness")
public class WYBusinessController {
	@Autowired
	WYBusinessServiceI wyBusinessService;
	private static final ObjectMapper objectMapper = new ObjectMapper();
	
	
	
	/******************各岗位共有功能********************/	
	/**
	 * 
	 * @Title: getScanInformation
	 * @Description: 扫一扫
	 * @param: @param type
	 * @param: @param code
	 * @param: @param token
	 * @param: @return     
	 * @return: String     
	 * @author:  xieyuanqiu
	 * @date: 2017年9月9日 下午4:25:11
	 * @throws
	 */
	@RequestMapping(value="/qrcode/{type}/{code}/{token}", method=RequestMethod.GET,produces={"text/html;charset=UTF-8"})
	@ResponseBody
	public String getScanInformation(@PathVariable("type") String type, @PathVariable("code") String code, 
			@PathVariable("token") String token){
		objectMapper.addMixInAnnotations(SysProject.class, SysProjectMixin.BasicInfo.class);
		ResultJson resultJson = wyBusinessService.getScanInformation(token, type, code);
		try {
			return objectMapper.writeValueAsString(resultJson);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * 报事
	 * @Title: addComplaint
	 * @Description: TODO
	 * @param: @param request
	 * @param: @param response
	 * @param: @param type
	 * @param: @param inspectionPoint
	 * @param: @param remarks
	 * @param: @param urgency
	 * @param: @param equipmentCode
	 * @param: @param token
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年9月12日 下午2:43:13
	 * @throws
	 */
	@RequestMapping(value="/complaint", method=RequestMethod.POST)
	@ResponseBody
	public ResultJson addComplaint(HttpServletRequest request,HttpServletResponse response,@RequestParam(value="type") String type,
			@RequestParam(value="inspectionPoint") String inspectionPoint, @RequestParam(value="remarks",required=false) String remarks,
			@RequestParam(value="urgency") String urgency,@RequestParam(value="equipmentCode",required=false) String equipmentCode,@RequestParam("token") String token){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("type", type);
		params.put("inspectionPoint", inspectionPoint);
		params.put("remarks", remarks);
		params.put("urgency", urgency);
		params.put("token", token);
		params.put("equipmentCode", equipmentCode);
		ResultJson resultJson = wyBusinessService.addComplaint(request, response, params);
		return resultJson;
	}
	
	/**
	 * 
	 * @Title: updateWYUser
	 * @Description: 修改个人信息
	 * @param: @param name
	 * @param: @param telephone
	 * @param: @param token
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年9月12日 上午11:10:55
	 * @throws
	 */
	@RequestMapping(value="/wyUser", method=RequestMethod.POST)
	@ResponseBody
	public ResultJson updateWYUser(@RequestParam(value="name",required=false) String name, @RequestParam(value="telephone",required=false) String telephone, 
			@RequestParam("token") String token){
		ResultJson resultJson = wyBusinessService.updateWYUser(token, name, telephone);
		return resultJson;
	}
	
	/**
	 * 
	 * @Title		:getDoorList
	 * @Description	:TODO
	 * @param		:@param ProjectCode
	 * @param		:@return
	 * @return		:ResultJson
	 * @author		:wujiayi
	 * @time		:2017年10月1日 上午11:18:08
	 * @throws
	 */
	@RequestMapping(value="/iot/entrance/getDoorList",method=RequestMethod.POST,produces={"text/html;charset=UTF-8"})
	@ResponseBody
	public String getDoorList(@RequestParam("wyAccountId") String wyAccountId,@RequestParam("projectId") String projectId){
		ResultJson resultJson = wyBusinessService.getDoorList(wyAccountId,projectId);
		objectMapper.addMixInAnnotations(SysProject.class, SysProjectMixin.BasicInfo.class);
/*		objectMapper.addMixInAnnotations(SysBuilding.class, SysBuildingMixin.BasicInfo.class);
		objectMapper.addMixInAnnotations(SysUnit.class, SysUnitMixin.BasicInfo.class);*/
		try {
			return objectMapper.writeValueAsString(resultJson);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * 蓝牙绑卡
	 * @Title: bindBluetoothCard
	 * @Description: TODO
	 * @param: @param name
	 * @param: @param telephone
	 * @param: @param token
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年10月8日 上午11:44:35
	 * @throws
	 */
	@RequestMapping(value="/bluetoothCard/bind", method=RequestMethod.POST)
	@ResponseBody
	public ResultJson bindBluetoothCard(@RequestParam("accountId") String accountId,@RequestParam("currentCardStr") String currentCardStr,@RequestParam("name") String name,
			@RequestParam("address") String address,@RequestParam("identity") String identity, @RequestParam(value="communitySign",required=false) String communitySign, 
			@RequestParam(value="version",required=false) String version, @RequestParam(value="model",required=false) String model,
			@RequestParam(value="rssi",required=false) String rssi){
		Map<Object,Object> params = new HashMap<>();
		params.put("accountId", accountId);
		params.put("identity", identity);
		params.put("currentCardStr", currentCardStr);
		params.put("name", name);
		params.put("address", address);
		params.put("communitySign", communitySign);
		params.put("version", version);
		params.put("model", model);
		params.put("rssi", rssi);
		ResultJson resultJson = wyBusinessService.bindBluetoothCard(params);
		return resultJson;
	}
	/**
	 * 获取蓝牙绑卡记录
	 * @Title: geBluetoothCardList
	 * @Description: TODO
	 * @param: @param token
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年10月8日 下午2:06:17
	 * @throws
	 */
	@RequestMapping(value="/bluetoothCard/{accountId}", method=RequestMethod.GET)
	@ResponseBody
	public ResultJson geBluetoothCardList(@PathVariable("accountId") String accountId){
		ResultJson resultJson = wyBusinessService.geBluetoothCardList(accountId);
		return resultJson;
	}
	/**
	 * 删除蓝牙记录
	 * @Title: delBluetoothCard
	 * @Description: TODO
	 * @param: @param address
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年10月8日 下午5:05:19
	 * @throws
	 */
	@RequestMapping(value="/delBluetoothCard", method=RequestMethod.POST)
	@ResponseBody
	public ResultJson delBluetoothCard(@RequestParam("address") String address,@RequestParam("accountId") String accountId){
		ResultJson resultJson = wyBusinessService.delBluetoothCard(address,accountId);
		return resultJson;
	}
	/************各岗位共有功能end**********/


	/*************安管主任*************/

	/**
	 * 获取所有报事详情（安防事件）
	 * @Title: listComplaint
	 * @Description: TODO
	 * @param: @param token
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年9月13日 下午4:53:13
	 * @throws
	 */

	@RequestMapping(value="/complaint/{token}", method=RequestMethod.GET)
	@ResponseBody
	public ResultJson listComplaint(@PathVariable("token") String token){
		ResultJson resultJson = wyBusinessService.listComplaint(token);
		return resultJson;
	}
	
	/**
	 * 提交安防待处理事件
	 * @Title: submitComplaint
	 * @Description: TODO
	 * @param: @param request
	 * @param: @param response
	 * @param: @param token
	 * @param: @param manageRemarks
	 * @param: @param workOrder
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年9月13日 下午6:04:33
	 * @throws
	 */
	@RequestMapping(value="/complaint/manage", method=RequestMethod.POST)
	@ResponseBody
	public ResultJson submitComplaint(HttpServletRequest request,HttpServletResponse response,@RequestParam("token")String token,
			@RequestParam(value="manageRemarks",required=false)String manageRemarks, @RequestParam("code")String workOrder){
		ResultJson resultJson = wyBusinessService.submitComplaint(request, response, token, manageRemarks, workOrder);
		return resultJson;
	}
	
	/**
	 * 获取访客登记记录
	 * @Title: getVisitorList
	 * @Description: TODO
	 * @param: @param token
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年9月14日 上午10:34:48
	 * @throws
	 */
	@RequestMapping(value="/visitor/{token}", method=RequestMethod.GET)
	@ResponseBody
	public ResultJson getVisitorList(@PathVariable("token")String token){
		ResultJson resultJson = wyBusinessService.getVisitorList(token);
		return resultJson;
	}
	/**
	 * 
	 * 获取停车场异常放行记录
	 * @Title: getParkingExceptionLog
	 * @Description: TODO
	 * @param: @param token
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年9月18日 上午11:31:17
	 * @throws
	 */
	@RequestMapping(value="/parking/out/exeption/{token}", method=RequestMethod.GET)
	@ResponseBody
	public ResultJson getParkingOutException(@PathVariable("token")String token){
		ResultJson resultJson = wyBusinessService.getParkingOutException(token);
		return resultJson;
	}
	
	/**
	 * 
	 * @Title: updateParkingOutExceptionStatus
	 * @Description: TODO
	 * @param: @param token
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年9月18日 下午5:34:00
	 * @throws
	 */
	@RequestMapping(value="/parking/out/exeption/handle", method=RequestMethod.POST)
	@ResponseBody
	public ResultJson updateParkingOutExceptionStatus(@RequestParam("token")String token, @RequestParam("exceptionId")String exceptionId, @RequestParam("status")String status){
		ResultJson resultJson = wyBusinessService.updateParkingOutExceptionStatus(token, exceptionId, status);
		return resultJson;
	}
	
	/**
	 * 接口根据车场ID，设备ID,时间段，状态查找出入场记录
	 * @Title: getParkingEntryLog
	 * @Description: TODO
	 * @param: @param token
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年9月18日 下午2:06:34
	 * @throws
	 */
	@RequestMapping(value="/parking/entryLog/{token}", method=RequestMethod.GET)
	@ResponseBody
	public ResultJson getParkingEntryLog(@PathVariable("token")String token){
		ResultJson resultJson = wyBusinessService.getParkingEntryLog(token);
		return resultJson;
	}
	/**
	 * 在线定位用
	 * @Title: getPatrolCollectNodes
	 * @Description: TODO
	 * @param: @param token
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年9月19日 下午4:57:10
	 * @throws
	 */
	@RequestMapping(value="/patrol/collectNodes/{token}", method=RequestMethod.GET)
	@ResponseBody
	public ResultJson getPatrolCollectNodes(@PathVariable("token")String token){
		ResultJson resultJson = wyBusinessService.getPatrolCollectNodes(token,6);
		return resultJson;
	}
	/**
	 * 获取巡更实时进度
	 * @Title: getPatrolCollectProgress
	 * @Description: TODO
	 * @param: @param token
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年9月19日 下午6:26:42
	 * @throws
	 */
	@RequestMapping(value="/patrol/progress/{token}", method=RequestMethod.GET)
	@ResponseBody
	public ResultJson getPatrolCollectProgress(@PathVariable("token")String token){
		ResultJson resultJson = wyBusinessService.getPatrolCollectProgress(token);
		return resultJson;
	}
	
	/************安管主任 end**********/

	/***************工程 岗位************/
	/**
	 * 
	 * @Title		:返回有权限的所有项目
	 * @Description	:TODO
	 * @param		:@param ProjectCode
	 * @param		:@param Cycle
	 * @param		:@param token
	 * @param		:@return
	 * @return		:ResultJson
	 * @author		:wujiayi
	 * @time		:2017年9月14日 上午9:26:00
	 * @throws
	 */
	@RequestMapping(value="/iot/machine/project/{token}",method=RequestMethod.GET)
	@ResponseBody
	public ResultJson getMachineProject(@PathVariable("token") String token){
		ResultJson resultJson = wyBusinessService.getMachineProject(token);
		return resultJson;
	}
	
	/**
	 * 
	 * @Title		:返回项目下所有设备信息
	 * @Description	:TODO
	 * @param		:@param ProjectCode
	 * @param		:@param token
	 * @param		:@return
	 * @return		:ResultJson
	 * @author		:wujiayi
	 * @time		:2017年9月14日 上午10:14:37
	 * @throws
	 */
	@RequestMapping(value="/iot/machine/device",method=RequestMethod.POST)
	@ResponseBody
	public ResultJson getMachineDevice(@RequestParam("ProjectCode") String ProjectCode,@RequestParam("token") String token){
		ResultJson resultJson = wyBusinessService.getMachineDevice(ProjectCode,token);
		return resultJson;
	}
	
	/**
	 * 
	 * @Title		:某项目当前设定周期内最新告警
	 * @Description	:TODO
	 * @param		:@param ProjectCode
	 * @param		:@param Cycle
	 * @param		:@param token
	 * @param		:@return
	 * @return		:ResultJson
	 * @author		:wujiayi
	 * @time		:2017年9月14日 上午9:26:00
	 * @throws
	 */
	@RequestMapping(value="/iot/machine/alarm", method=RequestMethod.POST)
	@ResponseBody
	public ResultJson getProjectCycleAlarm(@RequestParam("ProjectCode") String ProjectCode, @RequestParam("Cycle") String Cycle, 
			@RequestParam("token") String token){
		ResultJson resultJson = wyBusinessService.getProjectCycleAlarm(ProjectCode, Cycle, token);
		return resultJson;
	}
	
	/**
	 * 
	 * @Title		:某项目某日所有历史告警
	 * @Description	:TODO
	 * @param		:@param ProjectCode
	 * @param		:@param DeviceID
	 * @param		:@param Date
	 * @param		:@param token
	 * @param		:@return
	 * @return		:ResultJson
	 * @author		:wujiayi
	 * @time		:2017年9月14日 上午9:48:33
	 * @throws
	 */
	@RequestMapping(value="/iot/machine/alarmHistory", method=RequestMethod.POST)
	@ResponseBody
	public ResultJson getProjectSomeDayAlarm(@RequestParam("ProjectCode") String ProjectCode, @RequestParam("DeviceID") String DeviceID, 
			@RequestParam("Date") String Date, @RequestParam("token") String token){
		ResultJson resultJson = wyBusinessService.getProjectSomeDayAlarm(ProjectCode, DeviceID, Date, token);
		return resultJson;
	}
	
	/**
	 * 
	 * @Title		:返回项目所有房间与环境监测设备对应关系 
	 * @Description	:TODO
	 * @param		:@param ProjectCode
	 * @param		:@param Cycle
	 * @param		:@param token
	 * @param		:@return
	 * @return		:ResultJson
	 * @author		:wujiayi
	 * @time		:2017年9月14日 上午9:26:00
	 * @throws
	 */
	@RequestMapping(value="/iot/machine/navparame", method=RequestMethod.POST)
	@ResponseBody
	public ResultJson navparame(@RequestParam("ProjectCode") String ProjectCode,@RequestParam("token") String token){
		ResultJson resultJson = wyBusinessService.navparame(ProjectCode,token);
		return resultJson;
	}
	
	/**
	 * 
	 * @Title		:某项目某设备当前实时数据
	 * @Description	:TODO
	 * @param		:@param ProjectCode
	 * @param		:@param Cycle
	 * @param		:@param token
	 * @param		:@return
	 * @return		:ResultJson
	 * @author		:wujiayi
	 * @time		:2017年9月14日 上午9:26:00
	 * @throws
	 */
	@RequestMapping(value="/iot/machine/realdata", method=RequestMethod.POST)
	@ResponseBody
	public ResultJson getRealdata(@RequestParam("ProjectCode") String ProjectCode, @RequestParam("DeviceID") String DeviceID, 
			@RequestParam("token") String token){
		ResultJson resultJson = wyBusinessService.getRealdata(ProjectCode, DeviceID, token);
		return resultJson;
	}
	
	/**
	 * 
	 * @Title		:某项目某设备当前实时数据
	 * @Description	:TODO
	 * @param		:@param ProjectCode
	 * @param		:@param Cycle
	 * @param		:@param token
	 * @param		:@return
	 * @return		:ResultJson
	 * @author		:wujiayi
	 * @time		:2017年9月14日 上午9:26:00
	 * @throws
	 */
	@RequestMapping(value="/iot/machine/energy", method=RequestMethod.POST)
	@ResponseBody
	public ResultJson getEnergy(@RequestParam("ProjectCode") String ProjectCode, @RequestParam("Date") String Date, @RequestParam("token") String token){
		ResultJson resultJson = wyBusinessService.getEnergy(ProjectCode, Date, token);
		return resultJson;
	}
	
	
	/***************工程 岗位end************/
	
	
	/**********************************安管人员---门岗**********************************/
	
	/**
	 * 根据业主名称获取所拥有的单元信息
	 * @Title: getYZUnitByName
	 * @Description: TODO
	 * @param: @param token
	 * @param: @param yzName
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年9月21日 下午4:58:05
	 * @throws
	 */
	@RequestMapping(value="/unit", method=RequestMethod.POST)
	@ResponseBody
	public ResultJson getYZUnitByName(@RequestParam("token")String token,@RequestParam("yzName")String yzName){
		ResultJson resultJson = wyBusinessService.getYZUnitByName(token, yzName);
		return resultJson;
	}
	
	/**
	 * 访客登记
	 * @Title: visitorRegist
	 * @Description: TODO
	 * @param: @param name
	 * @param: @param type
	 * @param: @param certificateType
	 * @param: @param cetificateNo
	 * @param: @param telephone
	 * @param: @param timeUnit
	 * @param: @param timeNo
	 * @param: @param carNo
	 * @param: @param accessReason
	 * @param: @param token
	 * @param: @param roomNo
	 * @param: @param yzId
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年9月21日 下午4:57:51
	 * @throws
	 */
	@RequestMapping(value="/visitor/regist", method=RequestMethod.POST)
	@ResponseBody
	public ResultJson visitorRegist(@RequestParam("name") String name, @RequestParam("type") String type, @RequestParam("certificateType") String certificateType, 
			@RequestParam("cetificateNo") String cetificateNo, @RequestParam("telephone") String telephone, @RequestParam("timeUnit") String timeUnit,
			@RequestParam("timeNo") int timeNo,@RequestParam("carNo") String carNo,@RequestParam("accessReason") String accessReason,@RequestParam("token") String token,
			@RequestParam(value="roomNo",required=false) String roomNo,@RequestParam(value="yzId",required=false) String yzId){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("name", name);
		params.put("type", type);
		params.put("certificateType", certificateType);
		params.put("cetificateNo", cetificateNo);
		params.put("telephone", telephone);
		params.put("timeUnit", timeUnit);
		params.put("timeNo", timeNo);
		params.put("accessReason", accessReason);
		params.put("carNo", carNo);
		params.put("roomNo", roomNo);
		params.put("yzId", yzId);
		ResultJson resultJson = wyBusinessService.saveVisitor(token, params);
		return resultJson;
	}
}
